//
//  UIDevice+BaseFramework.m
//  LotteryClient
//
//  Created by Suny on 14-7-5.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "UIDevice+BaseFramework.h"
#include <sys/types.h>
#include <sys/sysctl.h>
#import <sys/utsname.h>

@implementation UIDevice (BaseFramework)

@end

@implementation UIDevice (Helpers)

- (BOOL)isRetinaDisplay
{
    if ([[UIScreen mainScreen] scale] > 1.0)
    {
        return YES;
    }
	
	return NO;
}

- (BOOL)is4InchRetinaDisplay
{
    if (CGRectGetHeight([[UIScreen mainScreen] bounds]) == 568.0) {
        return YES;
    }
    
    return NO;
}

- (BOOL)isWIFIConnected
{
//	if ([[SNNetWorkReachability reachabilityForLocalWiFi] currentReachabilityStatus] == SNReachableViaWiFi) {
//		return YES;
//	}
	
	return NO;
}

- (BOOL)isHaveNetWork
{
//    SNNetworkStatus  networkStatus = [[SNNetWorkReachability reachabilityForInternetConnection] currentReachabilityStatus];
//    if (networkStatus != SNNotReachable)
//    {
//		return YES;
//	}
	return NO;
}

+ (NSString *) platform
{
	size_t size;
	sysctlbyname("hw.machine", NULL, &size, NULL, 0);
	char *machine = malloc(size);
	sysctlbyname("hw.machine", machine, &size, NULL, 0);
	NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
	free(machine);
	return platform;
}

+ (NSString *)getReturnPlat:(NSString *)platform
{
    // http://theiphonewiki.com/wiki/Models
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 2G";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"iPhone 4 (CDMA)";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5C";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5C (Global)";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5S";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5S (Global)";
	
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch (1 Gen)";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch (2 Gen)";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch (3 Gen)";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch (4 Gen)";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";
    
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad1,2"])      return @"iPad 3G";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}

+ (NSString *) getStandardPlat
{
	return [self getReturnPlat:[self platform]];
}

+ (BOOL)isDeviceOrientationLandscape
{
    return [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ||
    [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight;
}

- (int)sn_majorVersion
{
    static int s_sn_uidevice_major_version = -1;
    if (s_sn_uidevice_major_version < 0)
        s_sn_uidevice_major_version = (int)[[self systemVersion] doubleValue];
    
    return s_sn_uidevice_major_version;
}

- (BOOL)sn_isIOS5OrEarlier
{
    return [self sn_majorVersion] <= 5;
}

- (BOOL)sn_isIOS6OrEarlier
{
    return [self sn_majorVersion] <= 6;
}

- (BOOL)sn_isIOS7OrLater
{
    return [self sn_majorVersion] >= 7;
}

- (BOOL)sn_shouldEnableTabBarBlurEffect
{
    return ([self sn_isIOS7OrLater]
            && [self sn_deviceModel] != kSNDeviceiPhone4
            );
}

- (NSString *) sn_deviceModelString
{
    static NSString * s_sn_uidevice_model_string = nil;
    if (!s_sn_uidevice_model_string)
    {
        struct utsname systemInfo;
        int ret = uname(&systemInfo);
        if (ret == 0)
        {
            s_sn_uidevice_model_string = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
        }
        else
        {
            s_sn_uidevice_model_string = @"Unknown";
        }
    }
    
    return s_sn_uidevice_model_string;
}

/**
 * http://theiphonewiki.com/wiki/Models
 */
- (SNDeviceModel) sn_deviceModel
{
    static SNDeviceModel s_sn_uidevice_model = -1;
//    if (s_sn_uidevice_model < 0)
//    {
//        s_sn_uidevice_model = kSNDeviceUnknown;
//        
//        @try {
//            NSArray * modelComponents = [[self sn_deviceModelString] componentsSeparatedByString:@","];
//            NSString * modelString = SNString([SNGlobalUtil trimString:[modelComponents objectAtIndexSafely:0]], nil);
//            
//            NSString * majorModel = modelString;
//            int majorModelGeneration = -1;
//            NSInteger index = NSNotFound;
//            for (int i = 0; i < [modelString length]; i++)
//            {
//                unichar c = [modelString characterAtIndex:i];
//                if ( c >= '0' && c <= '9')
//                {
//                    index = i;
//                    break;
//                }
//            }
//            
//            if (index != NSNotFound)
//            {
//                majorModel = [modelString substringToIndex:index];
//                majorModelGeneration = SNInt([modelString substringFromIndex:index], -1);
//            }
//            
//            
//            int minorModelNumber = SNInt([SNGlobalUtil trimString:[modelComponents objectAtIndexSafely:1]], -1);
//            
//            if ([majorModel isEqualToString:@"iPod"])
//            {
//                s_sn_uidevice_model = kSNDeviceUnknowniPod;
//                
//                if (majorModelGeneration == 1)
//                    s_sn_uidevice_model = kSNDeviceiPodTouch1;
//                else if (majorModelGeneration == 2)
//                    s_sn_uidevice_model = kSNDeviceiPodTouch2;
//                else if (majorModelGeneration == 3)
//                    s_sn_uidevice_model = kSNDeviceiPodTouch3;
//                else if (majorModelGeneration == 4)
//                    s_sn_uidevice_model = kSNDeviceiPodTouch4;
//                else if (majorModelGeneration == 5)
//                    s_sn_uidevice_model = kSNDeviceiPodTouch5;
//            }
//            else if ([majorModel isEqualToString:@"iPad"])
//            {
//                s_sn_uidevice_model = kSNDeviceUnknowniPad;
//                
//                if (majorModelGeneration == 1)
//                {
//                    s_sn_uidevice_model = kSNDeviceiPad1;
//                }
//                else if (majorModelGeneration == 2)
//                {
//                    if (minorModelNumber >= 1 && minorModelNumber <= 4)
//                        s_sn_uidevice_model = kSNDeviceiPad2;
//                    else if (minorModelNumber >= 5)
//                        s_sn_uidevice_model = kSNDeviceiPadMini;
//                }
//                else if (majorModelGeneration == 3)
//                {
//                    if (minorModelNumber >= 1 && minorModelNumber <= 3)
//                        s_sn_uidevice_model = kSNDeviceiPad3;
//                    else if (minorModelNumber >= 4)
//                        s_sn_uidevice_model = kSNDeviceiPad4;
//                }
//                else if (majorModelGeneration == 4)
//                {
//                    if (minorModelNumber >= 1 && minorModelNumber <= 3)
//                        s_sn_uidevice_model = kSNDeviceiPadAir;
//                    else if (minorModelNumber >= 4)
//                        s_sn_uidevice_model = kSNDeviceiPadMini2;
//                }
//            }
//            else if ([majorModel isEqualToString:@"iPhone"])
//            {
//                s_sn_uidevice_model = kSNDeviceUnknowniPhone;
//                
//                if (majorModelGeneration == 1)
//                    s_sn_uidevice_model = kSNDeviceiPhone1;
//                else if (majorModelGeneration == 2)
//                    s_sn_uidevice_model = kSNDeviceiPhone3GS;
//                else if (majorModelGeneration == 3)
//                    s_sn_uidevice_model = kSNDeviceiPhone4;
//                else if (majorModelGeneration == 4)
//                    s_sn_uidevice_model = kSNDeviceiPhone4S;
//                else if (majorModelGeneration == 5)
//                    s_sn_uidevice_model = kSNDeviceiPhone5;
//                else if (majorModelGeneration == 6)
//                    s_sn_uidevice_model = kSNDeviceiPhone5S;
//            }
//            else if ([modelString hasPrefix:@"x86"])
//            {
//                s_sn_uidevice_model = kSNDeviceSimulator;
//            }
//            
//			
//        }
//        @catch (NSException *exception) {
//            
//        }
//    }
    
    return s_sn_uidevice_model;
}
@end
